//Source file: D:\\test\\com\\ce\\pms\\custservice\\service\\impl\\CheckServiceImpl.java

package com.ce.pms.custservice.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.util.Assert;

import com.ce.pms.common.model.SysInfo;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.custservice.dao.ICheckRecordDao;
import com.ce.pms.custservice.model.AbstractQuestionaire;
import com.ce.pms.custservice.model.CheckRecordDO;
import com.ce.pms.custservice.model.CustServiceEventDO;
import com.ce.pms.custservice.model.QuestionAndAnswer;
import com.ce.pms.custservice.model.ReturnVisitRecordDO;
import com.ce.pms.custservice.service.ICheckService;
import com.ce.pms.custservice.service.IEventManageService;
import com.ce.pms.custservice.vo.AbstractQuestionaireVO;
import com.ce.pms.custservice.vo.CheckRecordVO;
import com.ce.pms.custservice.vo.QuestionAndAnswerVO;
import com.ce.pms.custservice.vo.ReturnVisitRecordVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.model.HouseDomain;
import com.ce.pms.system.custservice.model.ReturnVisitItemDO;
import com.ce.pms.system.custservice.model.ReturnVisitWayDO;
import com.ce.util.BeanUtil;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;
import com.sitechasia.webx.core.utils.base.PopulateUtil;
/***
 * 抽检服务实现
 * @author gaosong
 *
 */
public class CheckServiceImpl extends QuestionaireServiceImpl implements ICheckService
{

	/* (non-Javadoc)
	 * @see com.ce.pms.custservice.service.impl.QuestionaireServiceImpl#addQuestionair(java.lang.Long, com.ce.pms.custservice.vo.AbstractQuestionaireVO)
	 */
		/***
	    * 功能描述:  新增抽检<br>
	    * @param cRecordVO
	    * @param cRecordDO
	    * @return
	    * Create author:高嵩 <br>
	    * Create on:2008-12-23  <br>
	    * Edit author:  <br>
	    * Edit on:    <br>
	    * Why:     <br>
	    */
	@Override
	public void addQuestionair(Long csEventID, AbstractQuestionaireVO questionaireVO) {
		// TODO Auto-generated method stub
		Assert.notNull(csEventID,"customerEvent notNull");Assert.notNull(questionaireVO);
		//Assert.notEmpty(questionaireVO.getQuestionAndAnswerSet());
		CheckRecordDO checkRecordDO = new CheckRecordDO();
		this.assembleV2D(questionaireVO, checkRecordDO);

			for(QuestionAndAnswerVO questionAndAnswerVO : questionaireVO.getQuestionAndAnswerSet())
			{
				QuestionAndAnswer questionAndAnswer = new QuestionAndAnswer();
				this.assembleV2V(questionAndAnswerVO, questionAndAnswer);
				questionAndAnswer.setAbstraQuestionaire(checkRecordDO);
				//写入答题
				checkRecordDO.addAnswer(questionAndAnswer);
			}
			this.getCheckRecordDao().saveObject(checkRecordDO);
			//设置事件的抽检状态为“已抽检”
			this.getEventManageService().noteAddCheckRecord(csEventID);

	}
	/* (non-Javadoc)
	 * @see com.ce.pms.custservice.service.impl.QuestionaireServiceImpl#setAside(java.lang.Long)
	 */

		/***
	    * 功能描述:  作废抽检<br>
	    * @param cRecordVO
	    * @param cRecordDO
	    * @return
	    * Create author:高嵩 <br>
	    * Create on:2008-12-23  <br>
	    * Edit author:  <br>
	    * Edit on:    <br>
	    * Why:     <br>
	    */
	@Override
	public void setAside(AbstractQuestionaireVO questionaireVO) {
		// TODO Auto-generated method stub
		Assert.notNull(questionaireVO);
		Assert.notNull(questionaireVO.getKeyword(),"checkRecordID notNull");
		Assert.notNull(questionaireVO.getCustomerServiceEventID(),"custEventID notNull");

		CheckRecordDO checkRecordDO =  this.getCheckRecordDao().findById(questionaireVO.getKeyword());
		if(checkRecordDO == null){
			throw new BusinessException("custservice:checkRecordNotExistsException",BusinessException.WARNING);
		}
		//如果已作废抽检，则不能在次作废抽检
		if(checkRecordDO.getValidity().equals(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO)){
			throw new BusinessException("custservice:customerEvent_Cannot_SetAsideCheckException",BusinessException.WARNING);
		}
		checkRecordDO.setValidityFalse();
		checkRecordDO.setRemark(questionaireVO.getRemark());
		this.getCheckRecordDao().updateObject(checkRecordDO);
		//设置事件的抽检状态,重置抽检状态为空
		this.getEventManageService().noteSetAsideCheckRecord(questionaireVO.getCustomerServiceEventID());
	}


	private void assembleV2V(QuestionAndAnswerVO questionAndAnswerVO,
			QuestionAndAnswer questionAndAnswer) {
		if(null!=questionAndAnswerVO.getAnswer() && !"".equals(questionAndAnswerVO.getAnswer())){
			questionAndAnswer.setAnswer(questionAndAnswerVO.getAnswer());
		}
		if(null!=questionAndAnswerVO.getRemark() && !"".equals(questionAndAnswerVO.getRemark())){
			questionAndAnswer.setRemark(questionAndAnswerVO.getRemark());
		}
		if(null!=questionAndAnswerVO.getReturnVisitItemID() && !"".equals(questionAndAnswerVO.getReturnVisitItemID())){
			questionAndAnswer.setReturnVisitItemDO(new ReturnVisitItemDO(questionAndAnswerVO.getReturnVisitItemID()));
		}

	}

/**
    * @roseuid 49436CAE0109
    */
   public CheckServiceImpl()
   {

   }



   /***
    * 功能描述:  抽检VO转换成DO<br>
    * @param cRecordVO
    * @param cRecordDO
    * @return
    * Create author:高嵩 <br>
    * Create on:2008-12-23  <br>
    * Edit author:  <br>
    * Edit on:    <br>
    * Why:     <br>
    */
   public CheckRecordDO assembleV2D(AbstractQuestionaireVO cRecordVO, CheckRecordDO cRecordDO)
   {

	   cRecordDO.setClient(cRecordVO.getClient());

	   if(null!=cRecordVO.getHouseID()&&!"".equals(cRecordVO.getHouseID())){
		   cRecordDO.setHouse(new HouseDomain(cRecordVO.getHouseID()));
	   }
	   if(null!=cRecordVO.getHouseNumber()&&!"".equals(cRecordVO.getHouseNumber())){
		   cRecordDO.setHouseNumber(cRecordVO.getHouseNumber());
	   }
	   Assert.notNull(cRecordVO.getRecorder());
	   cRecordDO.setRecorder(cRecordVO.getRecorder());
	   cRecordDO.setRecordTime(cRecordVO.getRecordTime());
	   if(null!=cRecordVO.getSummarize() && !"".equals(cRecordVO.getSummarize())){
		   cRecordDO.setSummarize(cRecordVO.getSummarize());
	   }
	   if(null!=cRecordVO.getReturnVisitWayID() && !"".equals(cRecordVO.getReturnVisitWayID())){
		   cRecordDO.setReturnVisitWayDO(new ReturnVisitWayDO(cRecordVO.getReturnVisitWayID()));
	   }

	   cRecordDO.setCustomerServiceEventDO(new CustServiceEventDO(cRecordVO.getCustomerServiceEventID()));
	   Assert.notNull(cRecordVO.getCreator());
	   cRecordDO.setSysInfo(new SysInfo(cRecordVO.getCreator(),this.getCheckRecordDao().getDate()));


	   return cRecordDO;


   }

   /***
    * 功能描述: 抽检Domain转换成VO <br>
    * @param cRecordDO
    * @param cRecordVO
    * @return
    * Create author:高嵩 <br>
    * Create on:2008-12-23  <br>
    * Edit author:  <br>
    * Edit on:    <br>
    * Why:     <br>
    */
   public CheckRecordVO assembleD2V(AbstractQuestionaire cRecordDO, CheckRecordVO cRecordVO)
   {
	   	//Assert.isTrue(PopulateUtil.populate(cRecordDO, cRecordVO));
	    BeanUtil.convertVoAsDo(cRecordDO, cRecordVO);
	   if(!BlankUtil.isBlank(cRecordDO.getReturnVisitWayDO())){
		   cRecordVO.setReturnVisitWayID(cRecordDO.getReturnVisitWayDO().getKeyword());
	   }
	   if(!BlankUtil.isBlank(cRecordDO.getCustomerServiceEventDO())){
		   cRecordVO.setCustomerServiceEventID(cRecordDO.getCustomerServiceEventDO().getKeyword());
		   cRecordVO.setEventNo(cRecordDO.getCustomerServiceEventDO().getEventNo());
		   cRecordVO.setFullAddress(cRecordDO.getCustomerServiceEventDO().getFullAddress());
		   cRecordVO.setClient(cRecordDO.getClient());
	   }
	  if(!BlankUtil.isBlank(cRecordDO.getHouse())){
		  cRecordVO.setHouseID(cRecordDO.getHouse().getKeyword());
		  cRecordVO.setHouseNumber(cRecordDO.getHouseNumber());
	   }

	  if(null!=cRecordDO.getQuestionAndAnswerSet() && cRecordDO.getQuestionAndAnswerSet().size()!=0){
		  Set<QuestionAndAnswerVO> questionAndAnswerSet = new HashSet<QuestionAndAnswerVO>();
		  for(QuestionAndAnswer questionAndAnswer : cRecordDO.getQuestionAndAnswerSet())
			{
			  QuestionAndAnswerVO questionAndAnswerVO = new QuestionAndAnswerVO();
			  if(null!=questionAndAnswer.getAnswer() && !"".equals(questionAndAnswer.getAnswer())){
				  questionAndAnswerVO.setAnswer(questionAndAnswer.getAnswer());
				}
				if(null!=questionAndAnswer.getRemark() && !"".equals(questionAndAnswer.getRemark())){
					questionAndAnswerVO.setRemark(questionAndAnswer.getRemark());
				}
				if(null!=questionAndAnswer.getReturnVisitItemDO().getKeyword() && !"".equals(questionAndAnswer.getReturnVisitItemDO().getKeyword())){
					questionAndAnswerVO.setReturnVisitItemID(questionAndAnswer.getReturnVisitItemDO().getKeyword());
				}
				questionAndAnswerSet.add(questionAndAnswerVO);

			}
		  cRecordVO.setQuestionAndAnswerSet(questionAndAnswerSet);

	  }


		 return cRecordVO;
   }

   /**
	 * 功能描述:批量将抽检Domain转换为抽检VO<br>
	 * @param domainlist
	 * @return
	 * Create author:高嵩 <br>
	 * Create on:2008-12-19  <br>
	 */
	private List<CheckRecordVO> checkRecordDOToVO(List<CheckRecordDO> domainlist){
		if(domainlist == null || domainlist.size() == 0) return null;
		List<CheckRecordVO> voList = new ArrayList<CheckRecordVO>();
		for(Iterator<CheckRecordDO> iterator = domainlist.iterator(); iterator.hasNext();){
			voList.add(assembleD2V(iterator.next(), new CheckRecordVO()));
		}
		return voList;
	}


	/***
	 * 功能描述:  根据ID查询抽检登记<br>
	 * @param id
	 * @return CheckRecordVO
	 * @roseuid 494351DA0157
	 * Create author:高嵩 <br>
	 * Create on:2008-12-23  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
   public CheckRecordVO getCheckRecordByID(Long id)
   {
	   Assert.notNull(id);
	   CheckRecordDO checkRecordDO = this.getCheckRecordDao().findById(id);
	   if(BlankUtil.isBlank(checkRecordDO)){
			throw new BusinessException("custservice:checkRecordNotExistsException",BusinessException.WARNING);
		}
	   CheckRecordVO checkRecordVO = new CheckRecordVO();
	   this.assembleD2V(checkRecordDO, checkRecordVO);
	   return checkRecordVO;
   }

   /***
    * 功能描述:  查询抽检登记列表<br>
    * @param checkRecordVO
    * @return List<CheckRecordVO>
    * @roseuid 494351DA0159
    * Create author:高嵩 <br>
    * Create on:2008-12-23  <br>
    * Edit author:  <br>
    * Edit on:    <br>
    * Why:     <br>
    */
   public List<CheckRecordVO> queryCheckRecordList(CheckRecordVO checkRecordVO)
   {
    return null;
   }

   /***
    * 功能描述:  分页查询抽检登记<br>
    * @param checkRecordVO
    * @return Page
    * @roseuid 494351DA015B
    * Create author:高嵩 <br>
    * Create on:2008-12-23  <br>
    * Edit author:  <br>
    * Edit on:    <br>
    * Why:     <br>
    */
   public Page queryCheckRecordByPage(CheckRecordVO checkRecordVO)
   {
	   Assert.notNull(checkRecordVO);
	   Page page = this.getCheckRecordDao().queryCheckRecordByPage(checkRecordVO);
	   List<CheckRecordDO> checkRecordDOList = page.getResult();

	   List<CheckRecordVO> checkRecordVOList = checkRecordDOToVO(checkRecordDOList);
	   page.setResult(checkRecordVOList);
	   return page;

   }


	private ICheckRecordDao checkRecordDao;
	private IEventManageService eventManageService;
   /**
	 * @return the eventManageService
	 */
	public IEventManageService getEventManageService() {
		return eventManageService;
	}

	/**
	 * @param eventManageService the eventManageService to set
	 */
	public void setEventManageService(IEventManageService eventManageService) {
		this.eventManageService = eventManageService;
	}

/**
	 * @return the checkRecordDao
	 */
	public ICheckRecordDao getCheckRecordDao() {
		return checkRecordDao;
	}

	/**
	 * @param checkRecordDao the checkRecordDao to set
	 */
	public void setCheckRecordDao(ICheckRecordDao checkRecordDao) {
		this.checkRecordDao = checkRecordDao;
	}
}
